<?php
class KBClientAction
{

	function KBClientAction() {
		
	}
	
	
	function setVars(&$controller, $manager) {
		
		$this->rq = &new RequestData($_GET);
		$this->rp = &new RequestData($_POST);
		
		$this->entry_id    = &$controller->entry_id;
		$this->category_id = &$controller->category_id;
		$this->view_id     = &$controller->view_id;
		$this->msg_id      = &$controller->msg_id;
		
		
		if(!$this->entry_id) {
			$_SESSION['kb_category_id_'] = false;
		}
		
		// set category session, probably this category will be browsing
		if($this->category_id) {
			$_SESSION['kb_category_id_'] = $this->category_id;
		}
		
		if($this->entry_id) {
			$category_ids = $manager->getCategoryIdsByEntryId($this->entry_id);
			
			// no category in session trying to guess entry category
			if(empty($_SESSION['kb_category_id_'])) {
				$_SESSION['kb_category_id_'] = current($category_ids);
				//echo "<pre>"; print_r('Set Category: empty session'); echo "</pre>";
			
			// category not in session do the same as no session
			} elseif(!in_array($_SESSION['kb_category_id_'], $category_ids)) {
				$_SESSION['kb_category_id_'] = current($category_ids);
				//echo "<pre>"; print_r('Set Category: not in arary'); echo "</pre>";
			}
			
			//echo "<pre>"; print_r($category_ids); echo "</pre>";
			//echo "<pre>"; print_r($manager->categories); echo "</pre>";
		}
			
		
		$this->category_id = $_SESSION['kb_category_id_'];
	}
	
	
	function checkAuth($controller, $manager) {

        $auth = &new AuthPriv();
		// do auth by cookie if set
		if($auth->getCookie() && !$auth->isAuth() && $manager->use_registration) {
			$auth = new AuthPriv;
			$auth->setTable($manager->tbl->user);
			$auth->doAuthByCookie();
			$controller->go('all');
		}
		
		// check for inactivity time
		$auth_ended = false;
		if(!$auth->getCookie() && $auth->isAuth()) {
			$auth->spentTime($manager->getSetting('auth_expired'));
			if(!$auth->isAuth()) {
				$auth_ended = true;
				$manager->user_id = false;
				$manager->user_role_id = false;
				$manager->is_registered = false;
				$manager->setCategories();
			}
		}
		
		// registered only
		if(!$manager->is_registered && $manager->getSetting('kb_register_access')) {
			$not_private = array('register', 'login', 'password', 'confirm', 'success_go');
			if(!in_array($this->view_id, $not_private)) {
				$login_msg = ($auth_ended) ? 'authtime_enter' : 'enter';
				$controller->go('login', '', '', $login_msg);
			}
		}
		
		
		// check private
		$private_views = array('entry', 'comment', 'print', 'getfile', 'index');
		if(in_array($this->view_id, $private_views) && $this->msg_id != 'glossary' ) {
			
			$private = ($this->category_id) ? $manager->isPrivateCategory($this->category_id) : false;
			
			// category hidden  = for some role or private_policy = 1 (hidden)
			// user should not see this category at all
			if($private == 'hidden') {
				if($auth_ended) { $controller->go('login', $this->category_id, $this->entry_id, 'authtime_' . $this->view_id); } 
				else            { $controller->go(); }
				
			// display msg if private category and $auth_ended
			// if we see concrete article, not category listing
			} elseif($private == 'display' && $this->view_id != 'index') {
				$login_msg = ($auth_ended) ? 'authtime_' . $this->view_id : 'entry';
				$controller->go('login', $this->category_id, $this->entry_id, $login_msg);
			
			// if concrete article private (not category)
			// if user try to see it it will have empty screen (not good probably)
			// does not matter for role or not
			// we need think more here...
			} elseif(!$manager->is_registered && $this->entry_id) {
				if($manager->isPrivateEntry($this->entry_id)) {
					$login_msg = ($auth_ended) ? 'authtime_' . $this->view_id : 'entry';
					$controller->go('login', $this->category_id, $this->entry_id, $login_msg);
				}
			}
		}
	}
}
?>